import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import axios from 'axios'
import './permissions'
import * as echarts from './echarts'
Vue.prototype.$echarts = echarts
//设置路径的默认前缀
axios.defaults.baseURL = "http://192.168.1.100:8086"
//使用axios拦截器，为所有请求加上token
// axios.interceptors.request.use(function (config) {
//   var token = localStorage.getItem("token");
  //设置axios请求的拦截器
  axios.interceptors.request.use(config=>{
    //从localStorage获取令牌
    var token = localStorage.getItem("token");
    //判断是否获取token
    if (token){
      //把token放入请求头中
      config.headers.token=token;
    }
    return config;
  })
//   return config;
// })
//把axios挂载到vue对象
Vue.prototype.$http = axios;
//让Vue引入使用ElementUI
Vue.use(ElementUI);

Vue.config.productionTip = false

//路由之前拦截处理
router.beforeEach((to, from, next) => {
  //获取请求路径
  const path = to.path;
  //放行路径
  if (path === "/Login") {
    //next 让程序继续运行
    return next();
  }
  //获取token值
  const token = localStorage.getItem("token");
  // token不空，放行
  if (token) {
    //alert(next);
    //alert(to);
    // alert(111);
    next()
  } else {
    //为空 到登录页面
    next({ path: '/Login' })
  }
})

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
